package service.Impl;

import com.alibaba.fastjson.JSONObject;
import entity.ClassroomTable;
import mapper.ClassroomTableDao;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import service.ClassroomTableService;
import utils.ExcelImportUtils;
import utils.PageObject;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (ClassroomTable)表服务实现类
 *
 * @author makejava
 * @since 2022-01-04 11:19:17
 */
@Service("classroomTableService")
public class ClassroomTableServiceImpl implements ClassroomTableService {
    @Resource
    private ClassroomTableDao classroomTableDao;

    @Override
    public List<ClassroomTable> queryClassroomAll() {
        List<ClassroomTable> classroomTables = classroomTableDao.queryClassroomAll();
        return classroomTables;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param pkId 主键
     * @return 实例对象
     */
    @Override
    public ClassroomTable queryById(Integer pkId) {
        return this.classroomTableDao.queryById(pkId);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public PageObject queryAllByLimit(JSONObject object) {
        //当前第几页
        Integer page = object.getInteger("page");
        //每页几条
        Integer limit = object.getInteger("limit");
        //计算开始位置
        Integer startindex = (page - 1) * limit;
        List<ClassroomTable> classroomTables = classroomTableDao.queryAllByLimit(startindex, limit);
        //查询所有数据条数
        Integer selectcount = classroomTableDao.selectcount();
        PageObject pageObject = new PageObject();
        pageObject.setPageCount(page);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setPageCount(selectcount / limit);//总页数
        pageObject.setRecords(Collections.singletonList(classroomTables));//当前需展示数据
        return pageObject;
    }

    /**
     * 新增数据
     *
     * @param insertobj 实例对象
     * @return 实例对象
     */
    @Override
    public JSONObject insert(JSONObject insertobj) {
        JSONObject result = new JSONObject();
        ClassroomTable classroomTable = new ClassroomTable();
        classroomTable.setClassroomid(insertobj.getString("classroomid"));
        classroomTable.setClassroomname(insertobj.getString("classroomname"));
        classroomTable.setClasssize(insertobj.getString("classsize"));
        classroomTable.setKszw(insertobj.getString("kszw"));
        classroomTable.setXq(insertobj.getString("xq"));

        classroomTable.setPkId(insertobj.getInteger("pkId"));


        if (classroomTable.getPkId() == null || classroomTable.getPkId().equals(0)) {

            int insert = this.classroomTableDao.insert(classroomTable);
            if (insert == 1) {
                result.put("code", "0");
                result.put("msg", "数据插入成功");
            } else {
                result.put("code", "-1");
                result.put("msg", "插入数据失败,请核对");
            }
            return result;
        } else {
           int update= this.classroomTableDao.update(classroomTable);
            if (update == 1) {
                result.put("code", "0");
                result.put("msg", "数据修改成功");
            } else {
                result.put("code", "-1");
                result.put("msg", "修改数据失败,请核对");
            }
            return result;
        }
    }

    /**
     * 修改数据
     *
     * @param classroomTable 实例对象
     * @return 实例对象
     */
    @Override
    public ClassroomTable update(ClassroomTable classroomTable) {
        this.classroomTableDao.update(classroomTable);
        return this.queryById(classroomTable.getPkId());
    }

    /**
     * 通过主键删除数据
     *
     * @param pkId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pkId) {
        return this.classroomTableDao.deleteById(pkId) > 0;
    }

    @Override
    public JSONObject excelimport(MultipartFile file) throws Exception {
        List<ClassroomTable> list = new ArrayList<>();
        JSONObject result = new JSONObject();
        Workbook workbook = null;
        String fileName = file.getOriginalFilename();
        if (fileName.endsWith("xls")) {
            workbook = new HSSFWorkbook(file.getInputStream());
        } else if (fileName.endsWith("xlsx")) {
            workbook = new XSSFWorkbook(file.getInputStream());
        } else {
            throw new Exception("传输文件不是Excel文件!");
        }
        Sheet sheet = workbook.getSheet("Sheet1");
        int rows = sheet.getLastRowNum();
        if (rows == 0) {
            throw new Exception("行数不能为0");
        }
        int s = 0;
        for (int i = 1; i < rows + 1; i++) {
            //读取左上端单元格
            Row row = sheet.getRow(i);
            //行不为空
            if (row != null) {
                //读取cell
                ClassroomTable classroomTable = new ClassroomTable();
                //教师id
                String classroomid = ExcelImportUtils.getCellValue(row.getCell(0));
                classroomTable.setClassroomid(classroomid);
                //教师名称
                String classroomname = ExcelImportUtils.getCellValue(row.getCell(1));
                classroomTable.setClassroomname(classroomname);
                //容纳人数
                String classsize = ExcelImportUtils.getCellValue(row.getCell(2));
                classroomTable.setClasssize(classsize);
                int insert = classroomTableDao.insert(classroomTable);
                s++;
                list.add(classroomTable);//把实数据放入集合里
            }
        }
        if (s == rows) {
            result.put("code", "0");
            result.put("msg", "数据导入成功!");
        } else {
            result.put("code", "-1");
            result.put("msg", "数据导入失败!");
        }
        return result;
    }
}
